//
//  ChooseIndustryVC.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/15.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "ChooseIndustryVC.h"
#import "FriendGroup.h"
#import "CAChooseIndustryTableViewCell.h"


@interface ChooseIndustryVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *_dataScourceAry;
}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation ChooseIndustryVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self loadData];
    
    // Do any additional setup after loading the view from its nib.
}


#pragma mark -- UITableViewDelegate & DataSource 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, MAIN_WIDTH, 50))];
    view.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 25, 20, 20))];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[_dataScourceAry[section] name]]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(CGRectGetMaxX(imageView.frame) + 5, 20, 100, 30))];
    label.text = [_dataScourceAry[section] name];
    label.font = [UIFont systemFontOfSize:12];
    
    [view addSubview:imageView];
    [view addSubview:label];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataScourceAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    
    return ([_dataScourceAry[indexPath.section] child].count / 4 + 1) * 35 + 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"chooseIndustryCell";
    
    CAChooseIndustryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CAChooseIndustryTableViewCell" owner:self options:nil] lastObject];
    } 
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.modelAry = [NSMutableArray arrayWithArray:[_dataScourceAry[indexPath.section] child]];
    
    __weak __typeof(self) weakSelf = self;
    cell.chooseBlock = ^(Friend *fri){
        if (weakSelf.chooseIndustryVCBlock != nil) {
            weakSelf.chooseIndustryVCBlock(fri);
        }
    };

    return cell;
}

#pragma mark 加载数据
- (void)loadData
{
    
    [self showIndicator:@"正在加载数据,请稍候..."];
    
    __weak __typeof(self) weakSelf = self;
    [HTTPManager get:@"http://cleanerair.com.cn/Api/Menu/list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [weakSelf hideIndicator];
        
        if ([responseObject[@"msg"] isEqualToString:@"成功"]) {
            
            NSArray *dataArr = [responseObject objectForKey:@"date"];
            NSMutableArray *fgArray = [NSMutableArray array];
            for (NSDictionary *dict in dataArr) {
                FriendGroup *friendGroup = [FriendGroup friendGroupWithDict:dict];
                [fgArray addObject:friendGroup];
            }
            _dataScourceAry = fgArray;
        }
        
        [weakSelf.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [weakSelf hideIndicator];
        
    }]; 
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
